using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Framework.Library.Components.Users;

namespace Framework.Library.Components.Security
{
	public class AuthenticatedPage : Page
	{
		protected string returnUrl = string.Empty;
		public UserInfo CurrentUser
		{
			get
			{
				if (Context.Items["MEMBER_INFO"] != null) return (UserInfo) Context.Items["MEMBER_INFO"];
				return null;
			}
			set
			{
				Context.Items.Remove("MEMBER_INFO");
				Context.Items["MEMBER_INFO"] = value;
			}
		}
       
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			returnUrl = Server.UrlEncode(Request.RawUrl);
			if(AuthenticateUtility.IsAuthenticated())
			{
				if (CurrentUser == null) CurrentUser = UserController.GetUser(AuthenticateUtility.GetUsername());
				if (CurrentUser == null) Response.Redirect("/Login.aspx?returnurl=" + returnUrl);
			}else
			{
				Response.Redirect("/Login.aspx?returnurl=" + returnUrl);
			}
		}
	}
}
